package com.ubs.training.server.dao.impl.jdbc;

import com.ubs.training.server.dao.RatingDao;
import com.ubs.training.server.dao.model.Client;
import com.ubs.training.server.dao.model.Rating;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/21/12
 * Time: 6:30 AM
 * To change this template use File | Settings | File Templates.
 */
public class NamedParamRatingDaoIMpl implements RatingDao {

    DataSource ds;

    NamedParameterJdbcOperations ops;


    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
        ops = new NamedParameterJdbcTemplate(ds);
    }

    @Override
    public List<Rating> getAll() {
        return ops.query("select RatingID, ClientID, RatingAgencyID, CountryID, UpdateTime, RatingValue, CountrySpecific " +
                "from Ratings", Collections.EMPTY_MAP, new BeanPropertyRowMapper(Rating.class));
    }

    @Override
    public List<Rating> getByClientID(int clientID) {

        String query = "select RatingID, ClientID, RatingAgencyID, CountryID, UpdateTime, RatingValue, CountrySpecific " +
                "from Ratings where ClientID="+clientID;
        return ops.query(query, Collections.EMPTY_MAP, new BeanPropertyRowMapper(Rating.class));
    }

    @Override
    public Rating getById(int id) {
        return ops.queryForObject("select RatingID, ClientID, RatingAgencyID, CountryID, UpdateTime, RatingValue, CountrySpecific" +
                " from Ratings where RatingID=:RatingID", Collections.singletonMap("RatingID", id), new BeanPropertyRowMapper<Rating>(Rating.class));
    }

    @Override
    public void add(Rating rating) {
        //To change body of implemented methods use File | Settings | File Templates.
        int id = ops.queryForInt("select next value for cmstr_seq from dual", Collections.EMPTY_MAP);
        rating.setRatingID(id);
        ops.update("insert into Ratings values( :RatingID, :ClientID, :RatingAgencyID, :CountryID, :UpdateTime, :RatingValue, :CountrySpecific)",
                new BeanPropertySqlParameterSource(rating));
    }

    @Override
    public void update(Rating rating) {
        int id = rating.getRatingID();
        ops.update("update Ratings set ClientID=:ClientID, RatingAgencyID=:RatingAgencyID, CountryID=:CountryID," +
                "UpdateTime=:UpdateTime, RatingValue=:RatingValue, CountrySpecific=:CountrySpecific where RatingID=:RatingID",
                new BeanPropertySqlParameterSource(rating));
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean delete(Rating rating) {
        int id = rating.getRatingID();
        int count;
        count = ops.update("delete from ratings where RatingID=:id", Collections.singletonMap("id", id));
        if (count == 1){
            return true;
        }
        else{
            return false;
        }
    }
}
